package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domian.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoImplApi implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void updete(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }


    @Override
    public Integer count(Long userId) {
        Integer count = userInfoMapper.count(userId);
        return count;
    }

    @Override
    public List<UserInfo> findblackList(int page, int size, Long userId) {
        page = (page - 1) * size;
        List<UserInfo> list = userInfoMapper.findblackList(page, size, userId);
        return list;
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo info) {
        if (userIds == null || userIds.size() == 0){
            return null;
        }
        QueryWrapper qw = new QueryWrapper();
        //1、用户id列表
        qw.in("id",userIds);
        //2、添加筛选条件
        if(info != null) {
            if(info.getAge() != null) {
                qw.lt("age",info.getAge());
            }
            if(!StringUtils.isEmpty(info.getGender())) {
                qw.eq("gender",info.getGender());
            }
            if(!StringUtils.isEmpty(info.getNickname())) {
                qw.like("nickname",info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public List<UserInfo> findAll(Integer page, Integer pageSize) {
        /*Page<UserInfo> userInfoPage = new Page<>(page, pageSize);
        Page<UserInfo> userInfoPage1 = userInfoMapper.selectPage(userInfoPage, null);*/
        page = (page - 1) * pageSize;
        List<UserInfo> userInfoPage = userInfoMapper.findUserInfoPage(page, pageSize);
        return userInfoPage;
    }





}
